package com.serge.smartdacha.model;

import java.util.Map;

public class TRController extends Controller {

    private int temperature;

    private int humidity;

    private int minTemperature;

    private int errorCount;

    private boolean relayOn;

    public TRController(String id) {
        super(id);
    }

    @Override
    protected void init(Map<String, String> map) {
        setMinTemperature(getInt(map.get("MT"), -1));
        setTemperature(getInt(map.get("T"), -1));
        setHumidity(getInt(map.get("H"), -1));
        setRelayOn("1".equals(map.get("R")));
        setErrorCount(getInt(map.get("E"), -1));
    }

    public int getTemperature() {
        return temperature;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }

    public int getHumidity() {
        return humidity;
    }

    public void setHumidity(int humidity) {
        this.humidity = humidity;
    }

    public int getMinTemperature() {
        return minTemperature;
    }

    public void setMinTemperature(int minTemperature) {
        this.minTemperature = minTemperature;
    }

    public int getErrorCount() {
        return errorCount;
    }

    public void setErrorCount(int errorCount) {
        this.errorCount = errorCount;
    }

    public boolean isRelayOn() {
        return relayOn;
    }

    public void setRelayOn(boolean relayOn) {
        this.relayOn = relayOn;
    }

    public int getMaxProgress() {
        return 6;
    }

    public int toProgress(int minTemp, boolean relay) {
        if (minTemp < 0) {
            return relay ? getMaxProgress() : 0;
        } else {
            return minTemp / 5;
        }
    }

    public int toProgress(int temp) {
        return temp / 5;
    }

    public int toMinTemp(int progress) {
        if (progress == 0) {
            return -1;
        } else if (progress == getMaxProgress()) {
            return Integer.MAX_VALUE;
        } else {
            return progress * 5;
        }
    }

    public String toSmsCommand(int progress) {
        if (progress == 0) {
            return OFF;
        } else if (progress == getMaxProgress()) {
            return ON;
        } else {
            return "" + (progress * 5);
        }
    }
}
